home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / VideoToolbox 96.06.15 / (Utilities) / GrabDrivers / GrabVideoDrivers.c < prev    next >
C/C++ Source or Header  |  1995-10-27  |  7KB  |  201 lines

  1. /*
  2. GrabVideoDrivers.c
  3. Gets each active video driver and saves it in a file of the same name, using the
  4. driver's version number as a resource id. These files can later be examined in
  5. ResEdit, using the public domain CODE editor. You'll want to consult Apple's
  6. Designing Cards and Drivers, 3rd Ed., Addison Wesley.
  7.  
  8. The ResEdit CODE editor is a public domain file, for use with ResEdit,
  9. distributed by:
  10. Ira L. Ruben
  11. Apple Computer, Inc.
  12. 20525 Mariani Ave., MS: 37-A
  13. Cupertino, Ca. 95014
  14. Ira@Apple.Com
  15. ftp://ftp.apple.com/dts/mac/tools/resedit/
  16.  
  17. Copyright restrictions prevent us from distributing video drivers themselves,
  18. but this program makes it easy for you to get an accessible copy of all the
  19. video drivers used in your own computers.
  20.  
  21. The primary users of this program will probably be those who wish to enhance
  22. SetEntriesQuickly.c. I hope that people will share the fruits of their labors by
  23. sending me the enhancements for distribution as part of the VideoToolbox.
  24.  
  25. This program is also useful for copying the Mac IIsi video driver to the Mac
  26. IIci. Since that's a ROM-based driver I don't know any way to automatically
  27. determine its size, and instead this program uses a generous guess. Consequently
  28. the resulting resource is larger than it needs to be. When you install this
  29. resource in the System file of your Mac IIci the excess space is wasted (both on
  30. disk and in memory), but the excess is only about 1 kB, so it doesn't seem worth
  31. worrying about.
  32.  
  33. HISTORY:
  34. 2/89 dgp Wrote it as "video hacker II.c" using THINK "Light Speed" C 3.
  35. 12/30/92 dgp updated it to THINK C 5 and renamed it GrabVideoDrivers.c
  36. 7/29/94 dgp Eliminated use of "#s" printf format, since it's not supported by
  37.             Metrowerks CodeWarrior C.
  38. 9/5/94 dgp removed assumption in printf's that int==short.
  39. 6/8/95 dgp Made sure that Mac structs are always 68k aligned.
  40. */
  41. #include "VideoToolbox.h"
  42. //#include <Errors.h>
  43. //#include <Files.h>
  44. //#include <Resources.h>
  45. Handle GetDriverFromSlotManager(GDHandle device);
  46.  
  47. void main(void);
  48. void CopyDeviceDriver(GDHandle device);
  49. void AddResourceToFile(unsigned char *filename,unsigned char *name,ResType type
  50.     ,int id,Handle handle);
  51.  
  52. #if UNIVERSAL_HEADERS<2
  53.     enum{dRAMBasedMask=0x0040};    /* dCtlDriver is a handle (1) or pointer (0) */
  54. #endif
  55. #if PRAGMA_ALIGN_SUPPORTED || __MWERKS__
  56.     #pragma options align=mac68k
  57. #endif
  58.  
  59. typedef struct {
  60.     short flags;
  61.     short blanks[3];
  62.     short open;
  63.     short prime;
  64.     short control;
  65.     short status;
  66.     short close;
  67.     Str255 name;
  68. } VideoDriver;
  69. VideoDriver *GDDriverAddress(GDHandle device);
  70.  
  71. #if PRAGMA_ALIGN_SUPPORTED || __MWERKS__
  72.     #pragma options align=reset
  73. #endif
  74.  
  75. void main()
  76. {
  77.     GDHandle device;
  78.     int i;
  79.     
  80.     MaximizeConsoleHeight();
  81.     printf("Welcome to GrabVideoDriver.\n");
  82.     printf(BreakLines("This program cycles through all your video devices and copies "
  83.     "each driver into a resource file, suitable for subsequent examination "
  84.     "with ResEdit (with the CODE editor). This is primarily of interest for people "
  85.     "who want to enhance SetEntriesQuickly.c to support more video devices.\n",78));
  86.     for(i=0;i<10;i++){
  87.         device=GetScreenDevice(i);
  88.         if(device==NULL)break;
  89.         CopyDeviceDriver(device);
  90.     }
  91.     printf("\nDone.\n");
  92. }
  93.  
  94. void CopyDeviceDriver(GDHandle device)
  95. {
  96.     char *name,filename[32];
  97.     long driverSize;
  98.     AuxDCE **auxDCEHandle;
  99.     Handle handle;
  100.     VideoDriver *driver;
  101.     unsigned char *bytePtr;
  102.     int version;
  103.     
  104.     if(device==NULL)return;
  105.     printf("\n");
  106.     name=GDNameStr(device);
  107.     version=GDVersion(device);
  108.     printf("%s version %d.\n",name,version);
  109.     strncpy(filename,name,31);
  110.     // filename must be truncated to 31 characters */
  111.     filename[32]=0;
  112.     // Replace leading "." by "-" since the Mac filing system 
  113.     // is confused by filenames beginning with ".".
  114.     if(filename[0]=='.')filename[0]='-';
  115.  
  116.     driver=GDDriverAddress(device);
  117.     printf("Offsets: open 0x%x, prime 0x%x, control 0x%x, status 0x%x, close 0x%x\n"
  118.         ,driver->open,driver->prime,driver->control,driver->status,driver->close);
  119.     printf("name is at 0x%lx, ",driver->name-(unsigned char *) driver);
  120.     bytePtr=driver->name;
  121.     bytePtr += 1+bytePtr[0];    /* go to end of Pascal string */
  122.     bytePtr = (unsigned char *)((long)(bytePtr+1) & ~1);    // round up to word boundary
  123.     printf("version is at 0x%lx\n",bytePtr-(unsigned char *) driver);
  124.  
  125.     auxDCEHandle = (AuxDCE **) GetDCtlEntry((*device)->gdRefNum);
  126.     if((**auxDCEHandle).dCtlFlags & dRAMBasedMask){
  127.         printf("RAM-based driver.\n");
  128.         handle=(Handle)(**auxDCEHandle).dCtlDriver;
  129.         HandToHand(&handle);
  130.         printf("%ld bytes\n",GetHandleSize(handle));
  131.     }
  132.     else{
  133.         printf("ROM-based driver.\n");
  134.         handle=GetDriverFromSlotManager(device);
  135.         if(handle!=NULL){
  136.             printf("Got driver with help from Slot Manager.\n");
  137.             printf("%ld bytes\n",GetHandleSize(handle));
  138.         }else{
  139.             // We have a Ptr to the driver, but we don't know how big it is.
  140.             driverSize=(unsigned long) GetPtrSize((Ptr)(**auxDCEHandle).dCtlDriver);
  141.             if(driverSize==0 || MemError()){
  142.                 driverSize=driver->open;
  143.                 if(driverSize<driver->prime)driverSize=driver->prime;
  144.                 if(driverSize<driver->control)driverSize=driver->control;
  145.                 if(driverSize<driver->status)driverSize=driver->status;
  146.                 if(driverSize<driver->close)driverSize=driver->close;
  147.                 driverSize*=2;
  148.                 printf("Size unknown, guessing (generously) at %ld, twice the highest offset.\n",driverSize);
  149.             }else printf("%ld bytes\n",driverSize);
  150.             PtrToHand((**auxDCEHandle).dCtlDriver,&handle,driverSize);
  151.         }
  152.     }
  153.     if(handle!=NULL){
  154.         AddResourceToFile(c2pstr(filename),c2pstr(name),'DRVR',version,handle);
  155.         printf("Driver copied to “%s” file, using the version number %d as the resource id.\n"
  156.             ,p2cstr((unsigned char *)filename),version);
  157.         DisposHandle(handle);
  158.     }else printf("Couldn't copy driver.\n");
  159. }
  160.  
  161. /*
  162. This gets a copy of the driver from the slot manager. Returns NULL unless we can
  163. find exactly the same driver as is specified by the supplied GDHandle. We check
  164. every byte. This is not a useless operation, because although we already have
  165. the address of the driver, we don't necessarily already have its size, and the
  166. slot manager will supply us with a handle, from which we can obtain the size.
  167. */
  168. Handle GetDriverFromSlotManager(GDHandle device)
  169. {
  170.     SpBlock spBlock;
  171.     SEBlock sEBlock;
  172.     unsigned char *desiredName,name[256];
  173.     int error;
  174.     Ptr *handle;
  175.     
  176.     desiredName=GDName(device);
  177.     spBlock.spsExecPBlk = (Ptr) &sEBlock;
  178.     spBlock.spSlot = 0;
  179.     spBlock.spID = 0;
  180.     spBlock.spExtDev = 0;
  181.     while(1){
  182.         error = SNextSRsrc(&spBlock);
  183.         if(error==smNoMoresRsrcs) break;
  184.         if(error){
  185.             printf("SNextSRsrc error %d\n",error);
  186.             break;
  187.         }
  188.         spBlock.spResult = (unsigned long) &name;
  189.         error = SReadDrvrName(&spBlock);
  190.         if(!EqualString(desiredName,name,1,1))continue;
  191.         error = SGetDriver(&spBlock);
  192.         if(!error)continue;
  193.         handle = (Handle) spBlock.spResult;
  194.         if(memcmp(*handle,GDDriverAddress(device),GetHandleSize(handle))!=0){
  195.             DisposHandle(handle);
  196.             break;
  197.         }
  198.         return handle;
  199.     }
  200.     return NULL;
  201. }